package com.pz.cloud.framework.stdGroup.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMem;
import com.pz.cloud.framework.business.society.entity.OrgSociety;
import com.pz.cloud.framework.business.stdGroup.entity.OrgStdGroup;
import com.pz.cloud.framework.business.stdGroup.entity.OrgStdGroupUser;
import com.pz.cloud.framework.business.stdGroup.service.IOrgStdGroupService;
import com.pz.cloud.framework.business.stdGroup.service.IOrgStdGroupUserService;
import com.pz.cloud.framework.business.student.entity.Student;
import com.pz.cloud.framework.business.student.service.IStudentService;
import com.pz.cloud.framework.business.workflow.annotation.AssembleDealFlag;
import com.pz.cloud.framework.util.BeanUpdateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.BaseController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.BeanConvertUtil;
import org.jeecg.modules.system.entity.SysCheckRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@RequestMapping("org/stdGroupUser")
@RestController
@Api(tags = "学生组织成员表")
@Slf4j
public class OrgStdGroupUserController extends BaseController<OrgStdGroupUser, IOrgStdGroupUserService> {

    @Autowired
    private IOrgStdGroupService stdGroupService;
    @Autowired
    private IStudentService studentService;

    /**
     * 分页列表查询
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "学生组织成员-分页列表查询")
    @ApiOperation(value = "学生组织成员-分页列表查询", notes = "学生组织成员-分页列表查询")
    @GetMapping(value = "/list")
    @AssembleDealFlag
    @PermissionData(pageComponent = "organization/OrganizationMember")
    public Result<IPage<OrgStdGroupUser>> queryPageList(OrgStdGroupUser entity,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) {
        Result<IPage<OrgStdGroupUser>> result = new Result<>();
        IPage<OrgStdGroupUser> pageList = service.findPage(entity);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 分页列表查询
     *
     * @param entity
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "学生组织成员申请-分页列表查询")
    @ApiOperation(value = "学生组织成员申请-分页列表查询", notes = "学生组织成员申请-分页列表查询")
    @GetMapping(value = "/applyList")
    @AssembleDealFlag
    @PermissionData(pageComponent = "organization/NewMember")
    public Result<IPage<OrgStdGroupUser>> applyList(OrgStdGroupUser entity,
                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                        HttpServletRequest req) {
        Result<IPage<OrgStdGroupUser>> result = new Result<>();
        IPage<OrgStdGroupUser> pageList = service.findApplyPage(entity);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     * @return
     */
    @AutoLog(value = "学生组织成员-添加|编辑")
    @ApiOperation(value = "学生组织成员-添加|编辑", notes = "学生组织成员-添加|编辑")
    @PostMapping(value = "/add")
    public Result<OrgStdGroupUser> add(@RequestBody OrgStdGroupUser entity) {
        Result<OrgStdGroupUser> result = new Result<>();
        try {
            service.save(entity);
            result.success("保存成功！");
        } catch (Exception e) {
            if(e instanceof CustomRuntimeException){
                result.error500(e.getMessage());
            }else{
                result.error500("操作失败");
            }
            log.error(e.getMessage(), e);
        }
        return result;
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "学生组织成员-通过id删除")
    @ApiOperation(value = "学生组织成员-通过id删除", notes = "学生组织成员-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        try {
            service.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "学生组织成员-批量删除")
    @ApiOperation(value = "学生组织成员-批量删除", notes = "学生组织成员-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<OrgStdGroupUser> deleteBatch(@RequestParam(name = "ids") String ids) {
        Result<OrgStdGroupUser> result = new Result<OrgStdGroupUser>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            service.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "学生组织成员-通过id查询")
    @ApiOperation(value = "学生组织成员-通过id查询", notes = "职务表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<OrgStdGroupUser> queryById(@RequestParam(name = "id") String id) {
        Result<OrgStdGroupUser> result = new Result<OrgStdGroupUser>();
        OrgStdGroupUser orgStdGroupUser = service.getById(id);
        if (orgStdGroupUser == null) {
            result.error500("未找到对应实体");
        } else {
            OrgStdGroup byId = stdGroupService.getById(orgStdGroupUser.getStdGroupId());
            if(byId!=null){
                orgStdGroupUser.setStdGroupName(byId.getName());
            }
            result.setResult(orgStdGroupUser);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 导出excel
     *
     * @param request
     */
    @RequestMapping(value = "/exportXls")
    @PermissionData(pageComponent = "organization/OrganizationMember")
    public ModelAndView exportXls(HttpServletRequest request, OrgStdGroupUser bean) {
        bean.setPaging(false);
        IPage<OrgStdGroupUser> pageList = service.findPage(bean);
        return super.exportXls(request, pageList.getRecords(), OrgStdGroupUser.class, "校级组织成员");

    }


}
